package com.sgam.mf.web;

import java.util.Map;

import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.sgam.mf.helper.ProxyCurrentUser;
import com.sgam.mf.helper.RomanianNumberToWords;
import com.sgam.mf.reports.DownloadService;
import com.sgam.mf.reports.StatusResponse;
import com.sgam.mf.reports.TokenService;
import com.sgam.mf.service.sec.SecUserService;



@Controller
@RequestMapping("/reports")
public class ReportsController {
	
	protected final Logger log = Logger.getLogger(getClass());
	
	@Autowired private TokenService tokenService;
	
	@Autowired private DownloadService downloadService;
	
	@Autowired private SecUserService					secUserService;
	
	@RequestMapping(value="", method = RequestMethod.GET)
	public String generate(Map<String, Object> map) {
    	//Report generating stub;
		log.debug("======================================================");
		log.debug("======================================================");
		return "reportTester";
	} 
	 
		@RequestMapping(value="/download/token")
		public @ResponseBody StatusResponse getDownloadToken() {
			return new StatusResponse(true, tokenService.generate());
		}
		
		@RequestMapping(value="/download/progress")
		public @ResponseBody StatusResponse checkDownloadProgress(@RequestParam String token) {
			return new StatusResponse(true, tokenService.check(token));
		}
		
		@RequestMapping(value="/download")
		public void download(@RequestParam String type,
							 @RequestParam String token, 
							 @RequestParam(required=false) Integer orderId,
							 HttpServletResponse response) {
			downloadService.download(orderId, type, token, response);
		}
		
		@RequestMapping(value="/generateContractItext")
		public void generateContractItext(HttpServletResponse response, 
											@RequestParam(required=true) Integer contractId,
											@RequestParam(required=true) String  contractTemplateName, 
											@RequestParam(required=true) String  printBackground) {
			downloadService.generateContractItext(response, contractId, contractTemplateName, printBackground, secUserService.getProxyCurrentUser());
			
		}		

		@RequestMapping(value="/getReportContractsList")
		public void getReportContractsList(
							 HttpServletResponse response,
							 	@RequestParam(value="dateFrom", 		required=false) 	String  dateFrom,
								@RequestParam(value="dateTo", 			required=false) 	String  dateTo, 
								@RequestParam(value="regionId", 		required=false) 	String  regionId,
								@RequestParam(value="servicesString", 	required=true) 	String  servicesString
								) {
			//downloadService.download(orderId, type, token, response);
			if (dateFrom.length() <1)	{  dateFrom = "01.01.0001"; }
			if (dateTo.length() <1) 	{  dateTo = "01.01.9999"; }
			if (regionId.length() <1) 	{  regionId = "-1"; }
			
			downloadService.getReportContractsList(response, secUserService.getProxyCurrentUser(), "pdf", "token", dateFrom, dateTo,regionId,servicesString);
		}
		
		
		
		@RequestMapping(value="/getReportAsigurari")
		public void getReportAsigurari(
							 HttpServletResponse response,
							 	@RequestParam(value="dateFrom", 		required=false) 	String  dateFrom,
								@RequestParam(value="dateTo", 			required=false) 	String  dateTo
								) {
			//downloadService.download(orderId, type, token, response);
			if (dateFrom.length() <1)	{  dateFrom = "01.01.0001"; }
			if (dateTo.length() <1) 	{  dateTo = "01.01.9999"; }
			
			downloadService.getReportAsigurariList(response, secUserService.getProxyCurrentUser(), "pdf", "token", dateFrom, dateTo);
		}		
		 
		
		@RequestMapping(value="/getReportPayments")
		public void getReportPayments(
							 HttpServletResponse response,
							 	@RequestParam(value="dateFrom", 		required=false) 	String  dateFrom,
								@RequestParam(value="dateTo", 			required=false) 	String  dateTo,
								@RequestParam(value="pUtype", 			required=false) 	String  pUtype, //  тип юзера PRTNR, USR
								@RequestParam(value="pUid", 			required=false) 	String  pUid
								) {
			//downloadService.download(orderId, type, token, response);
			if (dateFrom.length() <1)	{  dateFrom = "01.01.0001"; }
			if (dateTo.length() <1) 	{  dateTo = "01.01.9999"; }
			
			downloadService.getReportPaymentsList(response, secUserService.getProxyCurrentUser(), "pdf", "token", dateFrom, dateTo, pUtype, pUid);
		}	
		
		
		@RequestMapping(value="/numberToText")
		public @ResponseBody String numberToText(HttpServletResponse response, 
											@RequestParam(required=true) Integer numberSuma) {
			String result = RomanianNumberToWords.convert(numberSuma);
			
			return result;
			
		}	
}
